﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CSF.BusinessCore.BLL;
using CSF.MultiLang.Data;
using Ict.DataAccess;

namespace CSF.MultiLang.BLL
{
    public class LangTextDefBLL : TransactionLogic<LangTextDef>
    {
        /// <summary>
        /// 插入一条多语言字符串定义，同时插入其字符串内容
        /// </summary>
        /// <param name="textDef"></param>
        /// <param name="contents"></param>
        /// <returns></returns>
        public bool InsertContents(LangTextDef textDef, IEnumerable<LangTextContent> contents)
        {
            bool result = false;

            using (Executor exec = DataContext.CreateExecutor())
            {
                try
                {
                    exec.BeginTransaction();

                    LangTextDefBLL textDefBll = new LangTextDefBLL();
                    textDefBll.Insert<LangTextDef>(textDef, null, exec);

                    int textid = textDef.Id;

                    LangTextContentBLL textContentBll = new LangTextContentBLL();
                    foreach (LangTextContent content in contents)
                    {
                        if (!string.IsNullOrWhiteSpace(content.Content))
                        {
                            content.TextId = textid;
                            content.CreateBy = textDef.CreateBy;
                            textContentBll.Insert<LangTextContent>(content, null, exec);
                        }
                    }

                    exec.CommitTransaction();
                    result = true;
                }
                catch
                {
                    exec.RollbackTransaction();
                    throw;
                }
            }
            return result;
        }

        /// <summary>
        /// 更新字符串内容
        /// </summary>
        /// <param name="contents"></param>
        /// <param name="textId"></param>
        /// <param name="modifyBy"></param>
        /// <returns></returns>
        public bool UpdateContents(IEnumerable<LangTextContent> contents, int textId, string modifyBy)
        {
            bool result = false;

            using (Executor exec = DataContext.CreateExecutor())
            {
                try
                {
                    exec.BeginTransaction();

                    LangTextContentBLL textContentBll = new LangTextContentBLL();
                    foreach (LangTextContent content in contents)
                    {
                        if (!string.IsNullOrWhiteSpace(content.Content))
                        {
                            if (content.Id > 0)
                            {
                                content.TextId = textId;
                                content.ModifyBy = modifyBy;
                                textContentBll.Update<LangTextContent>(content, null, p => p.Id == content.Id, exec);
                            }
                            else
                            {
                                content.TextId = textId;
                                content.CreateBy = modifyBy;
                                textContentBll.Insert<LangTextContent>(content, null, exec);
                            }
                        }
                        else
                        {
                            textContentBll.DeleteOneById(content.Id);
                        }
                    }
                    exec.CommitTransaction();
                    result = true;
                }
                catch
                {
                    exec.RollbackTransaction();
                    throw;
                }
            }

            return result;
        }
    }
}
